#include "show.h"
#include "ui_show.h"
#include <stdio.h>
#include <stdlib.h>

Show::Show(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Show)
{
    ui->setupUi(this);
}

Show::~Show()
{
    delete ui;
}

void Show::init(char imageName[])
{
    back=QImage(imageName);
    rate=1.0;
    color1=0;
    color2=255;
    drawPlace=0;
    startx=0;
    starty=0;
    mode=false;
    step_num=0;
    single_mode=true;

    w=back.width();
    h=back.height();

    data=(unsigned char **)malloc(sizeof(char*)*w);
    newdata=(unsigned int **)malloc(sizeof(int*)*w);
    for(int i=0;i<w;i++)
    {
        data[i]=(unsigned char *)malloc(sizeof(char)*h);
        memset(data[i],0,h);

        newdata[i]=(unsigned int *)malloc(sizeof(int)*h);
        memset(newdata[i],0,h*sizeof(int));
    }

    lmx=-1,lmy=-1,rmx=-1,rmy=-1;
}

void Show::keyPressEvent(QKeyEvent *ev)
{
    if(ev->key()==Qt::Key_F&&!ev->isAutoRepeat())
    {
        this->update();
    }
    else if(ev->key()==Qt::Key_B)
    {
        rate+=0.1;

        this->update();
    }
    else if(ev->key()==Qt::Key_S)
    {
        rate-=0.1;
        if(rate<0.1)
            rate=0.1;
//        this->resize(back.width()*rate,back.height()*rate);
        this->update();
    }
    else if(ev->key()==Qt::Key_1&&!ev->isAutoRepeat())
    {
        points[0].setX(lmx+startx);
        points[0].setY(lmy+starty);
        lmx=-1;
        this->update();
    }
    else if(ev->key()==Qt::Key_2&&!ev->isAutoRepeat())
    {
        if(mode)
        {
            points[1].setX(lmx+startx);
            points[1].setY(lmy+starty);
        }
        else
        {
            points[1].setX(points[0].x());
            points[1].setY(lmy+starty);
        }

        lmx=-1;
        this->update();
    }
    else if(ev->key()==Qt::Key_3&&!ev->isAutoRepeat())
    {
        if(points[0].x()==-1)
            return;
        Line l;
        l.p1=points[0];
        l.p2=points[1];
        l.c1=color1;
        l.c2=color2;
        if(points[1].y()<points[0].y())
        {
            l.p2=points[0];
            l.p1=points[1];
            l.c2=color1;
            l.c1=color2;
        }
        lines.push_back(l);

        if(mode)
            drawline_indata(l.p1.x(),l.p1.y(),l.p2.x(),l.p2.y(),l.c1,l.c2);
        else
            drawline_indata(l.p1.x(),l.p1.y(),l.p2.y()-l.p1.y(),l.c1,l.c2);

        points[0].setX(-1);
        points[1].setX(-1);

        step_num++;
        ui->pushButton->setText(tr("L:")+QString::number(lines.size()-drawPlace));
        this->update();
    }
    else if(ev->key()==Qt::Key_4&&!ev->isAutoRepeat())
    {
        QPoint p1,p2;
        for(int i=drawPlace;i<lines.size();i++)
        {
            p1=lines[i].p1;
            p2=lines[i].p2;
            if(mode)
                drawRect_indata(p1.x(),p1.y(),p2.x(),p2.y());
            else
                drawRect_indata(p1.x(),p1.y(),p2.y()-p1.y());
        }
        lastPlace=drawPlace;
        drawPlace=lines.size();
        this->update();

        show2.getPoints(this->data, this->w, this->h);
        show2.show();

        step_num++;

        ui->pushButton->setText(tr("L:")+QString::number(lines.size()-drawPlace));
        savedata("temp.png");
    }
    else if(ev->key()==Qt::Key_5&&!ev->isAutoRepeat())
    {
        QPoint p1,p2;
        for(int i=drawPlace;i<lines.size();i++)
        {
            p1=lines[i].p1;
            p2=lines[i].p2;
            drawRect_indata_h(p1.x(),p1.y(),p2.x(),p2.y());
        }
        lastPlace=drawPlace;
        drawPlace=lines.size();
        this->update();

        show2.getPoints(this->data, this->w, this->h);
        show2.show();

        step_num++;

        ui->pushButton->setText(tr("L:")+QString::number(lines.size()-drawPlace));
        savedata("temp.png");
    }
    else if(ev->key()==Qt::Key_6&&!ev->isAutoRepeat())
    {

        QPoint p1,p2;
        for(int i=drawPlace;i<lines.size();i++)
        {
            p1=lines[i].p1;
            p2=lines[i].p2;
            drawRect_indata_h1(p1.x(),p1.y(),p2.x(),p2.y());
        }
        lastPlace=drawPlace;
        drawPlace=lines.size();
        this->update();

        show2.getPoints(this->data, this->w, this->h);
        show2.show();

        step_num++;

        ui->pushButton->setText(tr("L:")+QString::number(lines.size()-drawPlace));
        savedata("temp.png");
    }
    else if(ev->key()==Qt::Key_7&&!ev->isAutoRepeat())
    {

        QPoint p1,p2;
        for(int i=drawPlace;i<lines.size();i++)
        {
            p1=lines[i].p1;
            p2=lines[i].p2;
            drawRect_indata_r(p1.x(),p1.y(),p2.x(),p2.y());
        }
        lastPlace=drawPlace;
        drawPlace=lines.size();
        this->update();

        step_num++;

        show2.getPoints(this->data, this->w, this->h);
        show2.show();

        ui->pushButton->setText(tr("L:")+QString::number(lines.size()-drawPlace));
        savedata("temp.png");
    }
    else if(ev->key()==Qt::Key_Z&&!ev->isAutoRepeat())
    {
//        lines.pop_back();
        back_toLast();
        this->update();
    }
    else if(ev->key()==Qt::Key_I)
    {
        starty--;
        if(starty<0)
            starty=0;
        this->update();
    }
    else if(ev->key()==Qt::Key_K)
    {
        if((h-starty)*rate>this->height())
            starty++;
        this->update();
    }
    else if(ev->key()==Qt::Key_J)
    {
        startx--;
        if(startx<0)
            startx=0;
        this->update();
    }
    else if(ev->key()==Qt::Key_L)
    {
        if((w-startx)*rate>this->width())
            startx++;
        this->update();
    }
    else if(ev->key()==Qt::Key_Q&&!ev->isAutoRepeat())
    {
        drawPlace=lines.size()-1;
        if(drawPlace<0)
            drawPlace=0;
        ui->pushButton->setText(tr("L:")+QString::number(lines.size()-drawPlace));
    }
}

void Show::paintEvent(QPaintEvent *)
{
    QPainter painter(this);

    if(back.height()*rate>1500)
    {
        painter.scale(rate, rate);
        int t1=this->width()/rate;
        int t2=this->height()/rate;
        int t3=startx+t1;
        int t4=starty+t2;
        if(t3>back.width())
            t3=back.width();
        if(t4>back.height())
            t4=back.height();
        QRectF target(startx, starty, t3-startx, t4-starty);
        QRectF target1(0, 0, t3-startx, t4-starty);
        painter.drawImage(target1, back, target);
        //this->resize(back.width()*rate,back.height()*rate);
    }
    else
    {
        painter.scale(rate, rate);
        startx=0;
        starty=0;
        QRectF target(0, 0, back.width(), back.height());
        painter.drawImage(target, back, target);
        this->resize(back.width()*rate,back.height()*rate);
    }


    QPen pen;
    pen.setWidth(1);

    for(int i=startx;i<w;i++)
    {
        for(int j=starty;j<h;j++)
        {
            if(data[i][j]!=0)
            {
                pen.setBrush(QColor(data[i][j],data[i][j],data[i][j]));
                painter.setPen(pen);
                painter.drawPoint(i-startx,j-starty);
            }
        }
    }

    if(lmx!=-1)
    {
        pen.setBrush(Qt::red);
        painter.setPen(pen);
        painter.drawRect(lmx-1,lmy-1,3,3);
    }

    if(rmx!=-1)
    {
        pen.setBrush(Qt::green);
        painter.setPen(pen);
        painter.drawRect(rmx-1,rmy-1,3,3);
    }

    if(points[0].x()!=-1){
        pen.setBrush(QColor(color1,color1,color1));
        painter.setPen(pen);
        painter.drawRect(points[0].x()-1-startx,points[0].y()-1-starty,3,3);
    }

    if(points[1].x()!=-1){
        pen.setBrush(QColor(color2,color2,color2));
        painter.setPen(pen);
        painter.drawRect(points[1].x()-1-startx,points[1].y()-1-starty,3,3);
    }
}

void Show::mousePressEvent(QMouseEvent *event)
{
    if(event->button() == Qt::LeftButton)
    {
        lmx=event->x()/rate;
        lmy=event->y()/rate;
        this->update();
    }
    else if(event->button() == Qt::RightButton)
    {
        rmx=event->x()/rate;
        rmy=event->y()/rate;
        ui->pushButton->setText(QString::number(data[rmx+startx][rmy+starty]));
        this->update();
    }
}


//    if(lines.size()>0)
//    {
//        QPoint p1,p2;
//        pen.setBrush(Qt::transparent);
//        painter.setPen(pen);
//        for(int i=0;i<lines.size();i++)
//        {
//            p1=lines[i].p1;
//            p2=lines[i].p2;
//            QLinearGradient linearGradient(p1.x(),p1.y(),p2.x(),p2.y());
//            //从点（100，150）开始到点（300，150）结束，确定一条直线
//            linearGradient.setColorAt(0,QColor(lines[i].c1,lines[i].c1,lines[i].c1));
//            linearGradient.setColorAt(1,QColor(lines[i].c2,lines[i].c2,lines[i].c2));
//            //将直线开始点设为0，终点设为1，然后分段设置颜色
//            painter.setBrush(linearGradient);
//            painter.drawRect(p1.x(),p1.y(),1,p2.y()-p1.y());
//        }
//    }
